import * as THREE from "three"
// 导入轨道控制器
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";

//方法
import { Content } from './content';
import { Gui } from './gui';
//创建场景
const scene = new THREE.Scene()
//创建相机
const camera = new THREE.PerspectiveCamera(
    75,
    window.innerWidth / window.innerHeight,
    0.1,
    1000
)
//设置相机
camera.position.set(1, 1, 5)
//camera.position.z = 5
camera.lookAt(0, 0, 0)
scene.add(camera)

//初始化渲染器
const renderer = new THREE.WebGLRenderer()
renderer.setSize(window.innerWidth, window.innerHeight)
document.body.appendChild(renderer.domElement)
//
// 创建轨道控制器(拖拽查看视角)
const cnotrols = new OrbitControls(camera, renderer.domElement)
//坐标辅助器
const axes = new THREE.AxesHelper(5)
scene.add(axes)
//创建纹理加载器
const textureLoader = new THREE.TextureLoader()

//------------------------------------------------

const content = new Content(THREE, scene, camera, cnotrols, renderer,textureLoader)
content.object()
content.texture()
//--控制器----------------------------------------------
const gui = new Gui()
gui.screen()
gui.wireframe(content.element().cubeParentMaterial)
gui.aoMapIntensity(content.element().planMaterial)
//------------------------------------------------
function animate () {
    cnotrols.update()
    requestAnimationFrame(animate)
    renderer.render(scene, camera)
}
animate()
//------------------------------------------------
// 监听画面变化，更新渲染画面
window.addEventListener("resize", () => {
    //跟新摄像头
    camera.aspect = window.innerWidth / window.innerHeight
    //跟新相机矩阵
    camera.updateProjectionMatrix()
    //跟新渲染器
    renderer.setSize(window.innerWidth, window.innerHeight)
    //   设置渲染器的像素比
    renderer.setPixelRatio(window.devicePixelRatio);
});
//全屏
window.addEventListener("dblclick", () => {
    const fullScreenElement = document.fullscreenElement;
    if (!fullScreenElement) {
        //   双击控制屏幕进入全屏，退出全屏
        // 让画布对象全屏
        document.body.requestFullscreen();
    } else {
        //   退出全屏，使用document对象
        document.exitFullscreen();
    }
}); 
